package com.pearadmin.pro.modules.not.rest;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.modules.not.domain.SysInbox;
import com.pearadmin.pro.modules.not.domain.SysInboxreply;
import com.pearadmin.pro.modules.not.service.SysInboxService;
import com.pearadmin.pro.modules.sys.domain.SysUser;
import io.swagger.annotations.Api;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.not.param.SysInboxRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * 私信控制器
 * <p>
 * Author: 就 眠 仪 式
 * CreateTime: 2021/03/28
 */
@Api(tags = {"私信"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "inbox")
public class SysInboxController extends BaseController {


    @Resource
    private SysInboxService sysInboxService;
    @Resource
    private UserContext userContext;

    /**
     * 查询私信
     *
     * @param request 查询参数
     * @return {@link Result}
     */
    @GetMapping("page")
    @Log(title = "查询私信")
    @ApiOperation(value = "查询私信")
    public Result page(SysInboxRequest request) {
        return success(sysInboxService.page(request));
    }

    /**
     * 新增私信
     *
     * @param sysInbox 私信实体
     * @return {@link Result}
     */
    @PostMapping("save")
    @Log(title = "新增私信")
    @ApiOperation(value = "新增私信")
    public Result save(@RequestBody SysInbox sysInbox) {
        sysInbox.setDialogueId(IdWorker.getIdStr());
        System.out.println("sysInbox："+sysInbox.toString());
        boolean save = sysInboxService.save(sysInbox);
        if(save){
            boolean a = sysInboxService.insertReply(sysInbox);
            return auto(a);
        }
        return auto(false);
    }

    /**
     * 修改私信
     *
     * @param sysInbox 私信实体
     * @return {@link Result}
     */
    @PutMapping("edit")
    @Log(title = "修改私信")
    @ApiOperation(value = "修改私信")
    public Result edit(@RequestBody SysInbox sysInbox) {
        return auto(sysInboxService.updateById(sysInbox));
    }

    /**
     * 删除私信
     *
     * @param id 私信编号
     * @return {@link Result}
     */
    @DeleteMapping("remove")
    @Log(title = "删除私信")
    @ApiOperation(value = "删除私信")
    public Result remove(@RequestParam String id) {
        return auto(sysInboxService.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param ids 私信编号
     * @return {@link Result}
     */
    @DeleteMapping("removeBatch")
    @Log(title = "批量删除")
    @ApiOperation(value = "批量删除")
    public Result removeBatch(@RequestParam List<String> ids) {
        return auto(sysInboxService.removeByIds(ids));
    }


    /**
     * 查询私信
     *
     * @param id 查询参数 对话编号
     * @return {@link Result}
     */
    @GetMapping("getLetterCount")
    @Log(title = "查询私信内容")
    @ApiOperation(value = "查询私信内容")
    public Result getLetterCount(@RequestParam String id) {


        Map<String, Object> returnMap = new HashMap<>();
        List<SysInboxreply> list = sysInboxService.getLetterCount(id);
        List<Map<String, String>> maps = new ArrayList<>();
        for (SysInboxreply sysInboxreply : list) {
            Map<String, String> map = new HashMap<>();
            String content = sysInboxreply.getContent();
            String createName = sysInboxreply.getCreateName();
            map.put("content", createName + "：" + content);
            maps.add(map);
        }
        returnMap.put("content", maps);
        returnMap.put("list", list);
        return success(returnMap);
    }


    /**
     * 私信回复
     *
     * @param sysInboxreply 回复实体类
     * @return com.pearadmin.pro.common.web.domain.Result
     * @author ZhouXingYun
     * @date 2022/5/9 18:47
     */
    @PutMapping("messageReply")
    @Log(title = "私信回复")
    @ApiOperation(value = "私信回复")
    public Result messageReply(@RequestBody SysInboxreply sysInboxreply) {
        boolean a = sysInboxService.messageReply(sysInboxreply);
        return auto(a);
    }

}
